from threading import Thread
import socket


def echo(client):
    while True:
        data = client.recv(1024)
        print(f"Received {data}")
        client.sendall(data)


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind(('127.0.0.1', 8080))
    server.listen(5)
    while True:
        # 阻塞，等待客户端连接
        client, address = server.accept()
        # 一旦客户端完成连接，创建一个线程来运行eaho函数
        thread = Thread(target=echo, args=(client,))
        # 开始运行线程
        thread.start()
